package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.SysRoleMapper;
import com.woniuxy.dao.SysUserMapper;
import com.woniuxy.domain.DataGridView;
import com.woniuxy.pojo.SysUser;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Constants;
import com.woniuxy.util.Md5;
import com.woniuxy.vo.UserVo;
import javafx.application.Application;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Override
    public SysUser getSysUserByLoginName(String loginname) {
        SysUser user=userMapper.getUserByLoginName(loginname);
        return user;
    }

    @Override
    public SysUser doLogin(String loginname, String password) {
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginname,password);
        try {
            subject.login(token);
            Session session=subject.getSession();
            SysUser user=this.getSysUserByLoginName(loginname);
            session.setAttribute("SESSION_USER",user);
            return user;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public DataGridView getUserListByPage(UserVo userVo) {
        Page<Object> page=PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<SysUser> data=this.userMapper.getListByProperty(userVo);
        return new DataGridView(page.getTotal(),data);
    }

    @Override
    public void addUser(UserVo userVo) {
        userVo.setPassword(Md5.md5s(userVo.getPassword()));
        this.userMapper.insertSelective(userVo);
    }

    @Override
    public void removeUser(Integer id) {
        this.userMapper.deleteByPrimaryKey(id);
        this.roleMapper.deleteRoleUserByUid(id);
    }

    @Override
    public void removeBatchUser(Integer[] ids) {
        for(Integer id:ids){
            this.removeUser(id);
        }
    }
    @Override
    public void updateUser(UserVo userVo) {
        this.userMapper.updateByPrimaryKeySelective(userVo);
    }

    @Override
    public void resetUserPwd(Integer id) {
        SysUser user=this.userMapper.selectByPrimaryKey(id);
        user.setPassword(Md5.md5s(Constants.DEFAULT_PWD));
        this.userMapper.updateByPrimaryKeySelective(user);
    }
    @Override
    public void assignRole(Integer[] rids, Integer uid) {
        this.roleMapper.deleteRoleByUid(uid);
        for(Integer rid:rids){
            this.roleMapper.insertUserRole(uid,rid);
        }
    }
}
